package com.brainshare.web.controller.an;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.brainshare.an.domain.AnWechatUserMechanism;
import com.brainshare.an.service.IAnWechatUserMechanismService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.brainshare.common.annotation.Log;
import com.brainshare.common.core.controller.BaseController;
import com.brainshare.common.core.domain.AjaxResult;
import com.brainshare.common.enums.BusinessType;
import com.brainshare.an.domain.AnWechatUser;
import com.brainshare.an.service.IAnWechatUserService;
import com.brainshare.common.utils.poi.ExcelUtil;
import com.brainshare.common.core.page.TableDataInfo;

/**
 * 用户数据Controller
 *
 * @author ruoyi
 * @date 2022-11-04
 */
@RestController
@RequestMapping("/an/wechat_user")
public class AnWechatUserController extends BaseController {
    @Autowired
    private IAnWechatUserService anWechatUserService;
    @Autowired
    private IAnWechatUserMechanismService anWechatUserMechanismService;

    /**
     * 查询用户数据列表
     */
    @PreAuthorize("@ss.hasPermi('an:wechat_user:list')")
    @GetMapping("/list")
    public TableDataInfo list(AnWechatUser anWechatUser) {
        startPage();
        List<AnWechatUser> list = anWechatUserService.selectAnWechatUserList(anWechatUser);

        return getDataTable(list);
    }

    /**
     * 查询用户报告列表
     */
    @PreAuthorize("@ss.hasPermi('an:wechat_user:list')")
    @GetMapping("/report/list")
    public TableDataInfo reportList(AnWechatUser anWechatUser) {
        List<AnWechatUserMechanism> anWechatUserMechanisms = anWechatUserMechanismService.selectWechatUserMechanismByWechatUserIds(Collections.singletonList(anWechatUser.getWechatUserId()));
        anWechatUserMechanisms = anWechatUserMechanisms.stream().filter(item -> item.getIsDone() != null && item.getIsDone() == 0).collect(Collectors.toList());
        anWechatUserMechanisms.forEach(item -> item.setScreeningResult(""));
        return getDataTable(anWechatUserMechanisms);
    }

    /**
     * 导出用户数据列表
     */
    @PreAuthorize("@ss.hasPermi('an:wechat_user:export')")
    @Log(title = "用户数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AnWechatUser anWechatUser) {
        List<AnWechatUser> list = anWechatUserService.selectAnWechatUserList(anWechatUser);
        ExcelUtil<AnWechatUser> util = new ExcelUtil<AnWechatUser>(AnWechatUser.class);
        util.exportExcel(response, list, "用户数据数据");
    }

    /**
     * 获取用户数据详细信息
     */
    @PreAuthorize("@ss.hasPermi('an:wechat_user:query')")
    @GetMapping(value = "/{wechatUserId}")
    public AjaxResult getInfo(@PathVariable("wechatUserId") Long wechatUserId) {
        return success(anWechatUserService.selectAnWechatUserByWechatUserId(wechatUserId));
    }

    /**
     * 新增用户数据
     */
    @PreAuthorize("@ss.hasPermi('an:wechat_user:add')")
    @Log(title = "用户数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AnWechatUser anWechatUser) {
        return toAjax(anWechatUserService.insertAnWechatUser(anWechatUser));
    }

    /**
     * 修改用户数据
     */
    @PreAuthorize("@ss.hasPermi('an:wechat_user:edit')")
    @Log(title = "用户数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AnWechatUser anWechatUser) {
        return toAjax(anWechatUserService.updateAnWechatUser(anWechatUser));
    }

    /**
     * 删除用户数据
     */
    @PreAuthorize("@ss.hasPermi('an:wechat_user:remove')")
    @Log(title = "用户数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{wechatUserIds}")
    public AjaxResult remove(@PathVariable Long[] wechatUserIds) {
        return toAjax(anWechatUserService.deleteAnWechatUserByWechatUserIds(wechatUserIds));
    }
}
